<template>
  <div class="app-container">
    <el-table
      v-loading="listLoading"
      element-loading-text="加载中"
      :data="tableData"
      style="width: 100%"
      border
      stripe
      size="mini"
      :header-cell-style="{ 'text-align': 'center' }"
      :cell-style="{ 'text-align': 'center' }"
    >
      <el-table-column label="序号" width="60">
        <template slot-scope="scope">
          {{ scope.$index + (currentPage - 1) * limit + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="头像" width="100">
        <template slot-scope="scope">
          <el-avatar
            shape="square"
            size="small"
            :src="server_URL + scope.row.avatar"
          ></el-avatar>
        </template>
      </el-table-column>
      <el-table-column prop="nickname" label="呢称" width="100">
      </el-table-column>
      <el-table-column prop="blog.title" label="评论文章" width="150">
      </el-table-column>
      <el-table-column prop="content" label="评论内容"> </el-table-column>
      <el-table-column label="评论时间" width="200">
        <template slot-scope="scope">
          {{ formatDate(scope.row.createDate) }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="250">
        <template slot-scope="scope">
          <el-button
            type="danger"
            size="mini"
            plain
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <div class="page-wrapper">
      <el-pagination
        background
        layout="total, sizes, prev, pager, next, jumper"
        :page-sizes="[10, 50, 80, 100]"
        :total="total"
        :current-page="currentPage"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { deleteComment, getPageComment } from '@/api/comment'
import { formatDate } from '@/utils/tools'
import { server_URL } from '@/urlConfig'
export default {
  data() {
    return {
      server_URL,
      tableData: [],
      total: 0,
      limit: 10, // 页容量
      currentPage: 1,
      listLoading: false
    }
  },

  created() {
    this.fetchData()
  },
  methods: {
    formatDate,
    async fetchData() {
      this.listLoading = true
      const resp = await getPageComment(this.currentPage, this.limit)
      this.tableData = resp.data.rows
      this.total = resp.data.total
      this.listLoading = false
    },
    async handleDelete(row) {
      this.$confirm('是否删除此条评论信息', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          await deleteComment(row.id)
          // 及时更新最新的页码，以避免异常状况
          const totalPage = Math.ceil((this.total - 1) / this.limit)
          const currentPage =
            this.currentPage > totalPage ? totalPage : this.currentPage
          this.currentPage = currentPage < 1 ? 1 : currentPage
          this.fetchData()
          this.$message({
            type: 'success',
            message: '删除成功'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 分页相关
    handleSizeChange(val) {
      this.limit = +val
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.currentPage = +val
      this.fetchData()
    }
  }
}
</script>

<style lang="scss" scoped>
.page-wrapper {
  margin-top: 30px;
  display: flex;
  justify-content: center;
}
</style>
